package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.MessageManager;
import service.impl.MessageManagerImpl;

public class ListMessageAction  extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageManager mm = new MessageManagerImpl();
		int messageCount=mm.getMessageCount();
		int pageCount=mm.getPageCount(messageCount, mm.MESSAGE_PAGE_SIZE);
		int pageNo;
		String pageNoStr=request.getParameter("pageNo");
		if(pageNoStr==null || pageNoStr.trim().equals("")){
			pageNo=1;
		}
		try{
			pageNo=Integer.parseInt(pageNoStr.trim());
		}catch(Exception e){
			pageNo=1;
		}
		if(pageNo>pageCount){
			pageNo=pageCount;
		}
		request.setAttribute("no" , (pageNo-1)*mm.MESSAGE_PAGE_SIZE);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("currentPage", pageNo);
		request.setAttribute("messageList", mm.getAllMessageByPage(pageNo));
		request.setAttribute("msg", mm.getMsg());
		request.getSession().setAttribute("PRE_URL",request.getHeader("Referer"));
		return mapping.findForward("listMessage");
	}

}
